
"""
（连续字符用 “字符 + 次数” 表示，如 "aabcccccaaa"→"a2b1c5a3"）
若压缩后长度不缩短则返回原字符串
"""
def compress_string(s):
    """字符串压缩，若压缩后长度不缩短则返回原字符串"""
    if not s:
        return ""
    result = []
    current_char = s[0]
    count = 1
    for char in s[1:]:
        if char == current_char:
            count += 1
        else:
            result.append(current_char + str(count))
            current_char = char
            count = 1
    # 处理最后一组字符
    result.append(current_char + str(count))
    compressed = ''.join(result)
    # 若压缩后长度更长，返回原字符串
    return compressed if len(compressed) < len(s) else s

# 测试
if __name__ == "__main__":
    s = "aabcccccaaa"
    print(compress_string(s))  # 输出: a2b1c5a3
    s = "abc"
    print(compress_string(s))  # 输出: abc（压缩后长度相同）
    s = ""
    print(compress_string(s))  # 输出: